package com.tiyende.kash.loop.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonViewHolderHelper
import com.lib.common.shapeview.layout.ShapeLinearLayout
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.common.CommonDialog
import com.tiyende.kash.loop.configs.TiyendeConfigs
import com.tiyende.kash.loop.databinding.TiyendeDialogGenderBinding
import com.tiyende.kash.loop.event.TiyendeEventPointType
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：性别选择dialog
 */
class TiyendeGenderDialog: CommonDialog {

    private lateinit var binding: TiyendeDialogGenderBinding
    private var mCallback: IDialogCallback? = null
    //开始选择时间
    private var startTime = 0L;
    private var itemAdapter: GenderItemAdapter? = null
    override fun initTiyendeWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    override fun show() {
        super.show()
        startTime = System.currentTimeMillis()
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogGenderBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initTiyendeView() {
        binding.recyclerViewGender.layoutManager = LinearLayoutManager(context)
        itemAdapter = GenderItemAdapter(binding.recyclerViewGender)
        binding.recyclerViewGender.adapter = this.itemAdapter
        binding.recyclerViewGender.itemAnimator = DefaultItemAnimator()
        this.itemAdapter?.setOnItemChildClickListener { parent, childView, position ->
            val item = itemAdapter?.getItem(position)
            item?.let {
                if (it.optBoolean("isSelected",false)){
                    return@setOnItemChildClickListener
                }
                itemAdapter?.let { adapter ->
                    for (i in 0..<adapter.itemCount){
                        if (i == position){
                            adapter.getItem(i)?.put("isSelected",true)
                        } else {
                            adapter.getItem(i)?.put("isSelected",false)
                        }
                    }
                }
            }
            itemAdapter?.notifyDataSetChanged()
            binding.btSubmit.isEnabled = queryTiyendeSelectedGenderItem() != null
        }
    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.btSubmit)
    }

    override fun loadTiyendeData() {
    }

    override fun onTiyendeViewClick(v: View?) {
        val selectedGender = queryTiyendeSelectedGenderItem() ?: return
        dismiss()
        if(mCallback != null){
            selectedGender.put("duration",System.currentTimeMillis() - startTime)
            mCallback?.onGenderSelectedItem(selectedGender)
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onGenderSelectedItem(data: JSONObject)
    }

    /**
     * 设置性别列表
     */
    fun addTiyendeGenderList(genderList: ArrayList<JSONObject>){
        itemAdapter?.data = genderList
        binding.btSubmit.isEnabled = queryTiyendeSelectedGenderItem() != null
    }

    /**
     * 获取选择的性别
     */
    private fun queryTiyendeSelectedGenderItem(): JSONObject?{
        itemAdapter?.data?.forEach {
            if (it.optBoolean("isSelected",false)){
                return it
            }
        }
        return null
    }

    internal inner class GenderItemAdapter(recyclerView: RecyclerView?) : CommonRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.tiyende_dialog_gender_item) {
        override fun setItemChildListener(helper: CommonViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.genderItemLayout) //点击Item
        }
        override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
            var showTitle = model.optString("title")
            if (TiyendeConfigs.isSwahili()){
                showTitle = model.optString("title_sw")
            }
            helper.setText(R.id.txtGenderTitle,String.format(getString(R.string.tiyende_gender_what),showTitle))
            val imageGenderIcon = helper.getImageView(R.id.imageGenderIcon)
            when(model.optString("value")){
                "MALE" -> {
                    imageGenderIcon.setImageResource(R.mipmap.tiyende_gender_male_icon)
                }
                else -> {
                    imageGenderIcon.setImageResource(R.mipmap.tiyende_gender_female_icon)
                }
            }

            val itemLayout = helper.getView<ShapeLinearLayout>(R.id.genderItemLayout)
            val imageGenderSelect = helper.getImageView(R.id.imageGenderSelect)
            if (model.optBoolean("isSelected",false)) {
                imageGenderSelect.setImageResource(R.mipmap.tiyende_gender_selected_icon)
                itemLayout.shapeDrawableBuilder
                    .setSolidColor(Color.parseColor("#FF000000"))
                    .setStrokeColor(Color.parseColor("#FF3ED9AB"))
                    .intoBackground()
            } else {
                imageGenderSelect.setImageResource(R.mipmap.tiyende_gender_normal_icon)
                itemLayout.shapeDrawableBuilder
                    .setSolidColor(Color.parseColor("#FF000000"))
                    .setStrokeColor(Color.parseColor("#FF000000"))
                    .intoBackground()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_GENDER_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_GENDER_DISMISS)
    }
}